﻿using LibSVMsharp;
using LibSVMsharp.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SVM_Simple_Classification
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        SVMProblem problem;      //= SVMProblemHelper.Load(@"dataset_path.txt");
        SVMProblem testProblem;  //= SVMProblemHelper.Load(@"test_dataset_path.txt");
        string g_train_file_path, g_test_file_path;
        string g_model;

        private void button_start_train_Click(object sender, EventArgs e)
        {
            SVMParameter parameter = new SVMParameter();
            parameter.Type = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C = 8.0;
            parameter.Gamma = 0.5;
            Timer timer=new Timer();
            DateTime time1 = new DateTime();
            DateTime time2 = new DateTime();
            //  SVMModel model = SVM.Train(problem, parameter);
            SVMModel model = SVM.LoadModel(g_model);
            double[] target = new double[testProblem.Length];
            // for (int i = 0; i < testProblem.Length; i++)
            //     target[i] = SVM.Predict(model, testProblem.X[i]);
            timer.Start();
            time1 = DateTime.Now;
            target[1543] = SVM.Predict(model, testProblem.X[1543]);
            time2 = DateTime.Now;
            timer.Stop();
            string using_time = (time2 - time1).TotalMilliseconds.ToString();
            target[5000] = SVM.Predict(model, testProblem.X[5000]);

            richTextBox_show.AppendText("检测结果： "+target[5000].ToString()+"\n");
            richTextBox_show.AppendText("用时： " + using_time+"\n\n");
           

       
            double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target);
        }

        private void 打开测试文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";           //打开.txt的文件
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择要打开的训练文件";


            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

                // FileStream fs = new FileStream(fileDialog.FileName, FileMode.Open, FileAccess.Read);
                string pathname = fileDialog.FileName;
                testProblem = SVMProblemHelper.Load(pathname);
                //  fs.Close();

                if (testProblem == null)
                {
                    MessageBox.Show("未选择文件");
                    return;
                }
            }
        }

        private void 打开模型ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "model文件(*.model)|*.model|所有文件(*.*)|*.*";           //打开.txt的文件
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择要打开的模型文件";


            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

                // FileStream fs = new FileStream(fileDialog.FileName, FileMode.Open, FileAccess.Read);
                 g_model = fileDialog.FileName;
               // problem = SVMProblemHelper.Load(pathname);
                //  fs.Close();

                if (g_model == null)
                {
                    MessageBox.Show("未选择文件");
                    return;
                }
            }

        }

        private void 打开训练文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";           //打开.txt的文件
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择要打开的训练文件";


            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

               // FileStream fs = new FileStream(fileDialog.FileName, FileMode.Open, FileAccess.Read);
                string pathname = fileDialog.FileName;
                problem = SVMProblemHelper.Load(pathname);
              //  fs.Close();

                if (problem == null)
                {
                    MessageBox.Show("未选择文件");
                    return;
                }
            }
            }
        }
}
